package com.fireline.beauty.member;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.bean.FwxmVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.fireline.beauty.seting.TcfwxmDlg;
import com.zgw.fireline.base.common.Assert;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 会员套餐办理
 * */
public class TcxfEdit implements IFunction {

	public int result = 0;
	private Shell shell;
	private Table tableNewItems;
	/**
	 * @wbp.nonvisual location=17,532
	 */
	private final Dataset ygxxSet = new Dataset(true, "ygxx",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=67,562
	 */
	private final Dataset hyxxSet = new Dataset(
			"select * from hyxx where \r\n hybh =?", SqliteJdbcImpl.INSTANCE);
	private Object hybh;
	private CLabel lblNewLabel;
	private CLabel label_3;
	private CLabel label_5;
	private CLabel label_6;
	/**
	 * @wbp.nonvisual location=147,562
	 */
	private final Dataset hytcxxSet = new Dataset(
			true,"hytcxm",
			SqliteJdbcImpl.INSTANCE);
	private Label labTip;

	public TcxfEdit(String hybh) {
		Assert.isNotNull(hybh);
		this.hybh = hybh;
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		ygxxSet.setKeyColumns(new String[] {});
		hyxxSet.setKeyColumns(new String[] {});
		hytcxxSet.setKeyColumns(new String[] { "tcbh", "xmbh" });
		Composite content = new Composite(parent, SWT.NONE);
		GridLayout gl_content = new GridLayout(1, false);
		gl_content.verticalSpacing = 0;
		content.setLayout(gl_content);

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		Group group = new Group(composite, SWT.NONE);
		group.setText("会员信息");
		GridLayout gl_group = new GridLayout(4, false);
		gl_group.marginBottom = 5;
		gl_group.marginWidth = 20;
		gl_group.horizontalSpacing = 0;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label label_1 = new Label(group, SWT.NONE);
		label_1.setText("会员编号：");

		lblNewLabel = new CLabel(group, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Label label_2 = new Label(group, SWT.NONE);
		label_2.setText("会员姓名：");

		label_3 = new CLabel(group, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_3.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Label label_4 = new Label(group, SWT.NONE);
		label_4.setText("联系电话：");

		label_5 = new CLabel(group, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_5.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Label label_7 = new Label(group, SWT.NONE);
		label_7.setText("卡内金额：");

		label_6 = new CLabel(group, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		label_6.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_6.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));

		Group group_1 = new Group(composite, SWT.NONE);
		FillLayout fl_group_1 = new FillLayout(SWT.HORIZONTAL);
		fl_group_1.marginWidth = 5;
		fl_group_1.marginHeight = 5;
		group_1.setLayout(fl_group_1);
		GridData gd_group_1 = new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1);
		gd_group_1.heightHint = 120;
		group_1.setLayoutData(gd_group_1);
		group_1.setText("原有项目");

		DataTable dataTable = new DataTable(group_1, SWT.BORDER
				| SWT.FULL_SELECTION, hytcxxSet);
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "xmbh");
		dataTableColumn.setText("项目编号");
		dataTableColumn.setWidth(70);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.NONE, "xmmc");
		dataTableColumn_1.setText("项目名称");
		dataTableColumn_1.setWidth(100);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(dataTable,
				SWT.NONE, "sycs");
		dataTableColumn_2.setText("剩余次数");
		dataTableColumn_2.setWidth(70);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(dataTable,
				SWT.NONE, "zq");
		dataTableColumn_3.setText("服务周期");
		dataTableColumn_3.setWidth(80);

		Group group_2 = new Group(composite, SWT.NONE);
		group_2.setLayout(new GridLayout(1, false));
		group_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		group_2.setText("新增项目");

		labTip = new Label(group_2, SWT.NONE);
		labTip.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		labTip.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labTip.setText("项目总计：0   实付金额：0");

		tableNewItems = new Table(group_2, SWT.BORDER | SWT.FULL_SELECTION);
		GridData gd_table_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_table_1.heightHint = 120;
		tableNewItems.setLayoutData(gd_table_1);
		tableNewItems.setLinesVisible(true);
		tableNewItems.setHeaderVisible(true);
		tableNewItems.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				doEditItem();
			}
		});
		TableColumn tableColumn = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn.setWidth(72);
		tableColumn.setText("项目编号");

		TableColumn tableColumn_1 = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("项目名称");

		TableColumn tableColumn_2 = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn_2.setWidth(66);
		tableColumn_2.setText("新增次数");

		TableColumn tableColumn_3 = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn_3.setWidth(51);
		tableColumn_3.setText("原价");

		TableColumn tableColumn_4 = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn_4.setWidth(53);
		tableColumn_4.setText("优惠价");

		TableColumn tableColumn_5 = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn_5.setWidth(62);
		tableColumn_5.setText("合计");

		TableColumn tableColumn_6 = new TableColumn(tableNewItems, SWT.NONE);
		tableColumn_6.setWidth(68);
		tableColumn_6.setText("实收");

		Composite composite_3 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(3, false);
		gl_composite_3.marginRight = 6;
		gl_composite_3.verticalSpacing = 0;
		gl_composite_3.marginWidth = 0;
		gl_composite_3.marginHeight = 0;
		gl_composite_3.horizontalSpacing = 0;
		composite_3.setLayout(gl_composite_3);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));

		ToolBar toolBar = new ToolBar(composite_3, SWT.FLAT | SWT.RIGHT);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doXf();
			}
		});
		tltmNewItem.setText("续费");

		ToolItem toolItem = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAddItem();
			}
		});
		tltmNewItem_1.setText("添加项目");

		ToolItem toolItem_2 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAddTc();
			}
		});
		tltmNewItem_2.setText("添加套餐");

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doRemove();
			}
		});
		tltmNewItem_3.setText("移除项目");

		Label lblNewLabel_1 = new Label(composite_3, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		lblNewLabel_1.setText("经办员工：");

		DataCombo dataCombo = new DataCombo(composite_3, SWT.READ_ONLY, ygxxSet);
		dataCombo.setShowText("${ygmc}");
		dataCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));

		Composite composite_1 = new Composite(content, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		composite_2.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite_2, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("保存");

		Button btnNewButton = new Button(composite_2, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				shell.close();
			}
		});
		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setBounds(0, 0, 72, 22);
		btnNewButton.setText("退出");
		setParams();
		createGUIAfter();
		setValues();

		return content;
	}

	/**
	 * 项目续费
	 * */
	protected void doXf() {
		if (hytcxxSet.getSelectedIndex() > -1) {
			TcfwxmDlg dlg = new TcfwxmDlg(shell, SWT.NONE,
					hytcxxSet.getString("xmbh"));
			if (dlg.open() == SWT.OK) {
				FwxmVo fwxm = dlg.fwxm;
				TableItem item = null;
				for (TableItem oldItem : tableNewItems.getItems()) {
					FwxmVo old = (FwxmVo) oldItem.getData();
					if (old.xmbh.equals(fwxm.xmbh)) {
						item = oldItem;
						break;
					}
				}
				if (item == null)
					item = new TableItem(tableNewItems, SWT.NONE);
				item.setText(new String[] { fwxm.xmbh, fwxm.xmmc,
						fwxm.fwcs + "", fwxm.dj, fwxm.yhdj, fwxm.hj, fwxm.ssje });
				item.setData(fwxm);
				tableNewItems.setSelection(item);
				setXfxx(fwxm);
				computeYfje();
			}
		}
	}

	protected void doRemove() {
		if (tableNewItems.getSelectionCount() > 0) {
			tableNewItems.getSelection()[0].dispose();
			computeYfje();
		} else {
			MessageDialog.openInformation(shell, "提示", "请选择要移除的项目");
		}
	}

	/**
	 * 添加套餐
	 * */
	protected void doAddTc() {
		XztcDialog dlg = new XztcDialog(shell, SWT.NONE);
		Dataset set = (Dataset) dlg.open();
		if (set != null) {
			for (int i = 0; i < set.getCount(); i++) {
				FwxmVo fwxm = new FwxmVo();
				set.setSelected(i);
				fwxm.bz = set.getString("bz");
				fwxm.dj = set.getString("dj");
				fwxm.fwcs = set.getValue("cs", Integer.class);
				fwxm.hlzq = set.getValue("zq", Integer.class);
				fwxm.yhdj = set.getString("yhdj");
				fwxm.xmbh = set.getString("xmbh");
				fwxm.xmmc = set.getString("xmmc");
				fwxm.hj = set.getString("hj");
				fwxm.ssje = set.getString("hj");
				fwxm.tcbh = set.getString("tcbh");
				setXfxx(fwxm);
				TableItem item = null;
				for (TableItem oldItem : tableNewItems.getItems()) {
					FwxmVo old = (FwxmVo) oldItem.getData();
					if (old.xmbh.equals(fwxm.xmbh)) {
						item = oldItem;
						break;
					}
				}
				if (item == null)
					item = new TableItem(tableNewItems, SWT.NONE);
				item.setText(new String[] { fwxm.xmbh, fwxm.xmmc,
						fwxm.fwcs + "", fwxm.dj, fwxm.yhdj, fwxm.hj, fwxm.ssje });
				item.setData(fwxm);
				tableNewItems.setSelection(item);

			}
			// 计算应付金额
			computeYfje();

		}
	}

	/**
	 * 添加项目
	 * */
	protected void doAddItem() {
		TcfwxmDlg dlg = new TcfwxmDlg(shell, SWT.NONE);
		if (dlg.open() == SWT.OK) {
			FwxmVo fwxm = dlg.fwxm;
			setXfxx(fwxm);
			TableItem item = null;
			for (TableItem oldItem : tableNewItems.getItems()) {
				FwxmVo old = (FwxmVo) oldItem.getData();
				if (old.xmbh.equals(fwxm.xmbh)) {
					item = oldItem;
					break;
				}
			}
			if (item == null)
				item = new TableItem(tableNewItems, SWT.NONE);

			item.setText(new String[] { fwxm.xmbh, fwxm.xmmc, fwxm.fwcs + "",
					fwxm.dj, fwxm.yhdj, fwxm.hj, fwxm.ssje });
			item.setData(fwxm);
			tableNewItems.setSelection(item);
			computeYfje();

		}
	}

	// 计算项目总额
	private BigDecimal computeYfje() {
		BigDecimal yfje = new BigDecimal("0");
		for (TableItem item : tableNewItems.getItems()) {
			FwxmVo fwxm = (FwxmVo) item.getData();
			yfje = yfje.add(new BigDecimal(fwxm.ssje));
		}
		String yfjeFormat = DecimalFormat.getCurrencyInstance().format(yfje);
		labTip.setText("项目总计：" + tableNewItems.getItemCount() + "   应付金额："
				+ yfjeFormat + "");
		labTip.getParent().layout();
		labTip.setData(yfje);
		return yfje;
	}

	/**
	 * 判断此FwxmVo 是否是续费信息 ，如果是则设置续标置
	 * 
	 * @return
	 * */
	private boolean setXfxx(FwxmVo fwxm) {
		for (int i = 0; i < hytcxxSet.getCount(); i++) {
			if (fwxm.xmbh.equals(hytcxxSet.getValueForIndex(i, "xmbh"))) {
				fwxm.dqsycs = hytcxxSet.getValueForIndex(i, "sycs",
						Integer.class);
				fwxm.isxf = true;
				return true;
			}
		}
		return false;
	}

	/**
	 * 修改项目
	 * */
	protected void doEditItem() {
		TableItem item = tableNewItems.getSelection()[0];
		TcfwxmDlg dlg = new TcfwxmDlg(shell, SWT.NONE);
		dlg.fwxm = (FwxmVo) item.getData();
		if (dlg.open() == SWT.OK) {
			FwxmVo fwxm = dlg.fwxm;
			item.setText(new String[] { fwxm.xmbh, fwxm.xmmc, fwxm.fwcs + "",
					fwxm.dj, fwxm.yhdj, fwxm.hj, fwxm.ssje });
			item.setData(fwxm);
			tableNewItems.setSelection(item);
		}
		computeYfje();
	}

	protected void setParams() {
		hyxxSet.setParam(0, hybh);
		hytcxxSet.setParam(0, hybh);
	}

	protected void setValues() {
		lblNewLabel.setText(hyxxSet.getValue("hybh", String.class, ""));
		label_3.setText(hyxxSet.getValue("hyxm", String.class, ""));
		label_5.setText(hyxxSet.getValue("lxdh", String.class, ""));
		label_6.setText(hyxxSet.getStringForExpr("${knje}元"));
	}

	protected void createGUIAfter() {
		hyxxSet.update();
		ygxxSet.update();
		hyxxSet.setSelected(0);
		hytcxxSet.update();
	}

	/**
	 * 执行流程
	 * <ol>
	 * <li>添加交费记录</li>
	 * <li>修改会员信息</li>
	 * <li>添加会员套餐交费</li>
	 * <li>修改会员套餐信息</li>
	 * </ol>
	 * */
	protected void doSave() {
		BigDecimal yfje = (BigDecimal) labTip.getData(); // 总费总额
		FkDialog dlg = new FkDialog(shell, SWT.NONE, 1);
		dlg.yfje = yfje.toString();
		if (dlg.open() == SWT.OK) {
			String ygbh = ygxxSet.getString("ygbh");
			BigDecimal dqje = new BigDecimal(hyxxSet.getString("knje"));
			if (dlg.fkfs.equals("czk")) {
				dqje = dqje.subtract(new BigDecimal(dlg.jfje));
				if (dqje.doubleValue() < 0) {
					MessageDialog.openInformation(shell, "提示",
							"卡内佘额不足请选择其它付款方式");
					return;
				}
			}
			Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);
			BatchItem batchItem = new BatchItem(batch);
			batchItem
					.setCommand(" insert into hyjf(id,hybh,rq,czy,yfje,jfje,yhje,sjje,yh,fkyh,qk,fkqk,zt,ztsm,tcbh,dqje,xj,xyk,djq,czk,bz,ygbh,fd)\r\n       values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			batchItem.setName("添加交费记录");
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
			SimpleDateFormat format2 = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String jfid = format.format(new Date());
			batch.setParam(0, 0, jfid);// id
			batch.setParam(0, 1, hybh);// hybh
			batch.setParam(0, 2, format2.format(new Date()));// rq
			batch.setParam(0, 3, ContextHelp.getUserName());// czy
			batch.setParam(0, 4, yfje.toString());// yfje 应付金额
			batch.setParam(0, 5, dlg.jfje);// jfje 交费金额
			batch.setParam(0, 6, "0");// yhje 优惠赠送金额
			batch.setParam(0, 7, "0");// sjje 实际入帐额
			batch.setParam(0, 8, "0"); // yh 优惠
			// fkyh 付款优惠
			batch.setParam(0, 9, dlg.fkyh);
			batch.setParam(0, 10, "0"); // qk 欠款
			// fkqk 付款欠款
			batch.setParam(0, 11, dlg.fkqk);
			batch.setParam(0, 12, "1"); // zt 状态即 付款类别
			batch.setParam(0, 13, "套餐续费");// ztsm 状态说明
			batch.setParam(0, 14, "");// tcbh套餐编号
			batch.setParam(0, 15, dqje.toString());// dqje卡内当前金额

			// xj 现金
			batch.setParam(0, 16, dlg.fkfs.equals("xj") ? dlg.jfje : "0");
			// xy 信用卡
			batch.setParam(0, 17, dlg.fkfs.equals("xyk") ? dlg.jfje : "0");
			// djq 代金券
			batch.setParam(0, 18, dlg.fkfs.equals("djq") ? dlg.jfje : "0");
			// czk 储值卡
			batch.setParam(0, 19, dlg.fkfs.equals("czk") ? dlg.jfje : "0");
			batch.setParam(0, 20, "");// bz 备注
			batch.setParam(0, 21, ygbh);// ygbh 员工编号
			batch.setParam(0, 22, "1");// fd

			/*
			 * 修改会员卡佘额
			 */
			BatchItem batchItem_1 = new BatchItem(batch);
			batchItem_1
					.setCommand("update hyxx set knje=?,hyjf=hyjf+?\r\nwhere hybh=?");
			batchItem_1.setName("修改会员卡佘额");
			batch.setParam(1, 0, dqje.toString());
			batch.setParam(1, 1, dlg.jfje);
			batch.setParam(1, 2, hybh);

			/*
			 * 添加会员套餐交费记录
			 */
			for (TableItem item : tableNewItems.getItems()) {
				FwxmVo fwxm = (FwxmVo) item.getData();
				BatchItem addTcjfjl = new BatchItem(batch);
				addTcjfjl.setName("添加会员套餐交费记录");
				addTcjfjl
						.setCommand("INSERT INTO hytcjf (jfbh, hybh, xmbh, cs, yhdj, zq, bz, dqcs, fd, ssje) VALUES(?,?,?,?,?,?,?,?,?,?)");
				addTcjfjl.setParam(0, jfid);
				addTcjfjl.setParam(1, hybh);
				addTcjfjl.setParam(2, fwxm.xmbh);
				addTcjfjl.setParam(3, fwxm.fwcs);
				addTcjfjl.setParam(4, fwxm.yhdj);
				addTcjfjl.setParam(5, fwxm.hlzq);
				addTcjfjl.setParam(6, fwxm.bz);
				addTcjfjl.setParam(7, fwxm.dqsycs);// 当前次数
				addTcjfjl.setParam(8, "1");
				addTcjfjl.setParam(9, fwxm.ssje);
				
				
				// 添加佘额变动记录
				BatchItem y = new BatchItem(batch);
				y.setName("添加佘额变动记录");
				y.setCommand("INSERT INTO yebd( hybh, lx, rq, je_inc, je_dec, cs_inc, cs_dec, sycs, syje, czy, bz, xmbh, fd, xfid)"
									+" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
				y.setParam(0, hybh);
				y.setParam(1, "套餐续费");
				y.setParam(2, format2.format(new Date()));
				y.setParam(3, "0");
				y.setParam(4, "0");
				y.setParam(5, fwxm.fwcs);
				y.setParam(6, "0");
				y.setParam(7, fwxm.dqsycs+fwxm.fwcs);
				y.setParam(8, dqje);
				y.setParam(9, ContextHelp.getUserName());
				y.setParam(10, "充值单号:"+jfid);
				y.setParam(11, fwxm.xmmc);
				y.setParam(12, "");
				y.setParam(13, jfid);
				
			}

			/*
			 * 修改会员套餐信息
			 */
			String addSql = "insert into hytc(hybh,tcbh,xmbh,cs,sycs,yhdj,zq,bz) values(?,?,?,?,?,?,?,?)";
			String xfSql = "update hytc set sycs=sycs+? where hybh=? and xmbh=?";
			for (TableItem item : tableNewItems.getItems()) {
				FwxmVo fwxm = (FwxmVo) item.getData();
				BatchItem batchItem_3 = new BatchItem(batch);
				if (fwxm.isxf) {
					batchItem_3.setName("会员套餐项目续费");
					batchItem_3.setCommand(xfSql);
					batchItem_3.setParam(0, fwxm.fwcs);
					batchItem_3.setParam(1, hybh);
					batchItem_3.setParam(2, fwxm.xmbh);
				} else {
					batchItem_3.setName("添加会员套餐项目");
					batchItem_3.setCommand(addSql);
					batchItem_3.setParam(0, hybh);
					batchItem_3.setParam(1, fwxm.tcbh);
					batchItem_3.setParam(2, fwxm.xmbh);
					batchItem_3.setParam(3, "0");
					batchItem_3.setParam(4, fwxm.fwcs);
					batchItem_3.setParam(5, fwxm.yhdj);
					batchItem_3.setParam(6, fwxm.hlzq);
					batchItem_3.setParam(7, fwxm.bz);
				}
			}
			
			try {
				batch.execute();
				result = SWT.OK;
				shell.close();
			} catch (Exception e) {
				e.printStackTrace();
				MessageDialog
						.openError(shell, "程序异常", "付款失败:" + e.getMessage());
			}

		}
	}
}
